package com.jiyun.service.impl;

import com.alibaba.csp.sentinel.util.StringUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jiyun.login.pojo.finance.Refueling;
import com.jiyun.login.pojo.finance.RefuelingVo;
import com.jiyun.mapper.RefuelingMapper;
import com.jiyun.result.Result;
import com.jiyun.service.IRefuelingService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author author
 * @since 2025-02-14
 */
@Service
public class RefuelingServiceImpl extends ServiceImpl<RefuelingMapper, Refueling> implements IRefuelingService {

    @Autowired
    private RefuelingMapper refuelingMapper;

    @Override
    public Result All(RefuelingVo refuelingVo) {
        Page<Refueling> refuelingPage = new Page<>(refuelingVo.getPages(), refuelingVo.getSize());
        QueryWrapper<Refueling> refuelingQueryWrapper = new QueryWrapper<>();
        if (!StringUtil.isEmpty(refuelingVo.getOwner())){
            refuelingQueryWrapper.eq("owner",refuelingVo.getOwner());
        }
        if (!StringUtil.isEmpty(refuelingVo.getFormer())&&!StringUtil.isEmpty(refuelingVo.getHoo())){
            refuelingQueryWrapper.between("creation_time",refuelingVo.getFormer(),refuelingVo.getHoo());
        }
        Page<Refueling> refuelingPage1 = refuelingMapper.selectPage(refuelingPage, refuelingQueryWrapper);
        return Result.ok(refuelingPage1);
    }

    @Override
    public Result findAdd(Refueling refueling) {
        Refueling refueling1 = new Refueling();
        if (refueling != null) {
            BeanUtils.copyProperties(refueling, refueling1);
        }
        // 设置新属性
        refueling1.setOperator("林江");
        refueling1.setCreationTime(LocalDateTime.now());
        int insert = refuelingMapper.insert(refueling1); // 插入 refueling1
        // 添加日志输出以确认插入的数据
        System.out.println("Inserted Refueling: " + refueling1);
        // 查询刚刚插入的记录并返回
        Refueling insertedRefueling = refuelingMapper.selectById(refueling1.getId());
        return Result.ok(insertedRefueling);
    }

    @Override
    public Result delete(Integer id){
        refuelingMapper.deleteById(id);
        return Result.ok();
    }


}
